// import * as THREE from "three"
let THREE=require('three');
import MaterialMgr from "./MaterialMgr";
import {ProductMgr} from "./ProductMgr";

export class SupLoader {

  constructor(supUrl, fbxUrl) {
    console.log("SupLoader constructor");
    this.supUrl = supUrl;
    this.fbxUrl = fbxUrl;
    this.materialMgr = new MaterialMgr();
    this.productMgr = new ProductMgr();
  }

  //旧版本加载supfile,fbx,texture
  init(onComplete) {

    let loader = new THREE.FileLoader(THREE.DefaultLoadingManager);
    loader.setResponseType('arraybuffer');
    let url = this.supUrl;
    let fbxurl = this.fbxUrl;
    let mgr = this;
    loader.load(url, function (buffer) {
      let content = String.fromCharCode.apply(null, new Uint8Array(buffer))
      // console.log('supUrl:'+url);
      // console.log('content:'+content);
      let obj = (JSON.parse(content));
      mgr.materialMgr.createMaterial(obj.MaterialNodes);
      mgr.productMgr.load(obj.Products, fbxurl);
      // mgr.productMgr.loadJson(obj.Products, fbxurl);
      onComplete();
    },null, function (error) {
      // onError(error);
      console.log("加载sup文件失败！");
      console.log(error);
    });
  }

}
